const fn = {}

/**
 * 手机号校验
 * @param {*} phoneNumber 手机号
 * @returns
 */
fn.isValidPhone = phoneNumber => {
  const regExp = /^1[3456789]\d{9}$/
  return regExp.test(phoneNumber)
}

/**
 * 16进制颜色转rgba
 * @param {*} hex
 * @param {*} alpha 透明度
 * @returns
 */
fn.hexToRgba = function (hex, alpha) {
  const r = parseInt(hex.substring(1, 3), 16)
  const g = parseInt(hex.substring(3, 5), 16)
  const b = parseInt(hex.substring(5, 7), 16)
  return `rgba(${r}, ${g}, ${b}, ${alpha})`
}

/**
 * 将原对象的属性赋值到目标对象
 * @param {*} target
 * @param {*} source
 */
fn.assignTargetObject = (target, source) => {
  for (let key in source) {
    if (target.hasOwnProperty(key)) {
      target[key] = source[key]
    }
  }
}

/**
 * 判断奇偶数
 * @param {*} num
 * @returns
 */
fn.isOdd = num => {
  return num % 2 === 1 || num % 2 === -1
}

/**
 * 获取dom元素信息
 * @param {*} proxy
 * @param {*} dom
 */
fn.getDomInfo = async (proxy, dom) => {
  const query = uni.createSelectorQuery().in(proxy)

  return new Promise(resolve => {
    query
      .select(dom)
      .fields({
        node: true, // 获取节点的 node 实例
        size: true, // 获取节点的宽高
        rect: true, // 获取节点的位置信息
        scrollOffset: true // 获取节点的滚动位置
      })
      .exec(res => {
        if (res && res[0]) {
          resolve(res[0])
        }
      })
  })
}

/**
 * 获取时间戳
 */
fn.getTimeStamp = time => {
  const date = new Date(Number(time))
  const year = date.getFullYear()
  const month =
    date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  return `${year}-${month}-${day}`
}

/**
 * 获取年月日时分秒时间戳
 */
fn.getTimeStampOne = time => {
  const date = new Date(Number(time) * 1000)
  const year = date.getFullYear()
  const month =
    date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  const hour = date.getHours()<10? '0'+date.getHours() : date.getHours(); // 获取小时数
  const minute = date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes(); // 获取分钟数
  const second = date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds(); // 获取秒数
  return `${year}-${month}-${day}  ${hour}:${minute}:${second}`
}


export default fn
